package com.infodms.dms.actions.claim.oldPart;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.infodms.dms.bean.AclUserBean;
import com.infodms.dms.bean.ClaimOldPartOutPreListBean;
import com.infodms.dms.bean.ClaimOldPartOutStoreLogBean;
import com.infodms.dms.common.Constant;
import com.infodms.dms.common.ErrorCodeConstant;
import com.infodms.dms.common.getCompanyId.GetOemcompanyId;
import com.infodms.dms.dao.claim.oldPart.ClaimOldPartOutStorageDao;
import com.infodms.dms.exception.BizException;
import com.infodms.dms.util.CommonUtils;
import com.infoservice.mvc.context.ActionContext;
import com.infoservice.mvc.context.RequestWrapper;
import com.infoservice.po3.bean.PageResult;
/**
 * 类说明：索赔旧件管理--索赔旧件出库查询
 * 作者：  赵伦达
 */
public class ClaimOldPartOutStorageManager {
	public Logger logger = Logger.getLogger(ClaimOldPartOutStorageManager.class);
	private ActionContext act = null;
	private AclUserBean logonUserBean = null;
	private RequestWrapper request=null;
	private ClaimOldPartOutStorageDao dao=null;
	//url导向
	private final String queryOutStoreUrl = "/jsp/claim/oldPart/queryOutStorageList.jsp";
	private final String queryAddOutStoreUrl = "/jsp/claim/oldPart/queryPreOutStorageList.jsp";
	
	/**
	 * Function：索赔旧件出库查询--初始化
	 * @param  ：	
	 * @return:		 
	 * @throw：	
	 * LastUpdate：	2010-6-17
	 */
	public void queryListPage(){
		try {
			act=ActionContext.getContext();
			logonUserBean=(AclUserBean) act.getSession().get(Constant.LOGON_USER);
			act.setForword(queryOutStoreUrl);
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE, "索赔旧件出库查询--初始化");
			logger.error(logonUserBean, e1);
			act.setException(e1);
		}
	}
	/**
	 * Function：索赔旧件出库查询--条件查询
	 * @param  ：	
	 * @return:		 
	 * @throw：	
	 * LastUpdate：	2010-6-17
	 * 
	 * 2010-10-12 修改 加入产地限制
	 */
	@SuppressWarnings("unchecked")
	public void queryOutListByCondition(){
		try {
			act=ActionContext.getContext();
			request = act.getRequest();
			logonUserBean=(AclUserBean) act.getSession().get(Constant.LOGON_USER);
			Long companyId=GetOemcompanyId.getOemCompanyId(logonUserBean);
			dao=ClaimOldPartOutStorageDao.getInstance();
			Map params=new HashMap<String,String>();
			// 处理当前页
			Integer curPage = request.getParamValue("curPage") != null ? Integer
					.parseInt(request.getParamValue("curPage")) : 1;
					
			String yieldly = request.getParamValue("yieldly");//产地
			String yieldlys = CommonUtils.findYieldlyByPoseId(logonUserBean.getPoseId());       //该用户拥有的产地权限
			
			params.put("company_id",companyId);
			params.put("supply_name", request.getParamValue("supply_name"));
			params.put("out_start_date",  request.getParamValue("out_start_date"));
			params.put("out_end_date",  request.getParamValue("out_end_date"));
			params.put("part_name",  request.getParamValue("part_name"));
			params.put("part_code",  request.getParamValue("part_code"));
			params.put("yieldly", yieldly);
			params.put("yieldlys", yieldlys);
			
			PageResult<ClaimOldPartOutStoreLogBean> ps=dao.getOutStorelogList(params, curPage, Constant.PAGE_SIZE);
			act.setOutData("ps", ps);
			act.setForword(queryOutStoreUrl);
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE, "索赔旧件出库查询--条件查询");
			logger.error(logonUserBean, e1);
			act.setException(e1);
		}
	}
	/**
	 * Function：索赔旧件出库查询--新增出库页面
	 * @param  ：	
	 * @return:		 
	 * @throw：	
	 * LastUpdate：	2010-6-17
	 */
	public void addPageList(){
		try {
			act=ActionContext.getContext();
			logonUserBean=(AclUserBean) act.getSession().get(Constant.LOGON_USER);
			
			//取得该用户拥有的产地权限
			String yieldly = CommonUtils.findYieldlyByPoseId(logonUserBean.getPoseId());
			
			act.setOutData("yieldly", yieldly);
			act.setForword(queryAddOutStoreUrl);
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE, "索赔旧件出库查询--新增出库页面");
			logger.error(logonUserBean, e1);
			act.setException(e1);
		}
	}
	/**
	 * Function：索赔旧件出库查询--查询出库信息
	 * @param  ：	
	 * @return:		 
	 * @throw：	
	 * LastUpdate：	2010-6-17
	 */
	@SuppressWarnings("unchecked")
	public void queryPreOutStoreList(){
		try {
			act=ActionContext.getContext();
			request = act.getRequest();
			logonUserBean=(AclUserBean) act.getSession().get(Constant.LOGON_USER);
			dao=ClaimOldPartOutStorageDao.getInstance();
			Long companyId=GetOemcompanyId.getOemCompanyId(logonUserBean);
			Map params=new HashMap<String,String>();
			// 处理当前页
			Integer curPage = request.getParamValue("curPage") != null ? Integer
					.parseInt(request.getParamValue("curPage")) : 1;
			
			String supply_name=request.getParamValue("supply_name");
			String part_code=request.getParamValue("part_code");
			String part_name=request.getParamValue("part_name");
			String yieldly = request.getParamValue("yieldly");//产地
			String yieldlys = CommonUtils.findYieldlyByPoseId(logonUserBean.getPoseId());       //该用户拥有的产地权限
			
			params.put("company_id",companyId);
			params.put("supply_name", supply_name);
			params.put("part_code", part_code);
			params.put("part_name", part_name);
			params.put("yieldly", yieldly);
			params.put("yieldlys", yieldlys);
			
			PageResult<ClaimOldPartOutPreListBean> ps=dao.getPreOutStoreList(params, curPage, Constant.PAGE_SIZE);
			act.setOutData("ps", ps);
			act.setForword(queryAddOutStoreUrl);
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE, "索赔旧件出库查询--查询出库信息");
			logger.error(logonUserBean, e1);
			act.setException(e1);
		}
	}
	/**
	 * Function：索赔旧件出库查询--出库操作
	 * @param  ：	
	 * @return:		 
	 * @throw：	
	 * LastUpdate：	2010-6-18
	 */
	public void outOfStore(){
		try {
			act=ActionContext.getContext();
			request = act.getRequest();
			logonUserBean=(AclUserBean) act.getSession().get(Constant.LOGON_USER);
			String idStr=request.getParamValue("idStr");
			int updateNum=0;
			dao=ClaimOldPartOutStorageDao.getInstance();
			updateNum=dao.saveOutOfStoreLogOper(request,logonUserBean);			
			if(updateNum==idStr.split(",").length){
				act.setOutData("updateResult","updateSuccess");
			}else{
				act.setOutData("updateResult","updateFailure");
			}
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.ADD_FAILURE_CODE, "索赔旧件出库查询--出库操作");
			logger.error(logonUserBean, e1);
			act.setException(e1);
		}
	}
	/**
	 * Function：索赔旧件出库查询--全部出库操作
	 * @param  ：	
	 * @return:		 
	 * @throw：	
	 * LastUpdate：	2010-6-18
	 */
	public void allStore(){
		try {
			act=ActionContext.getContext();
			request = act.getRequest();
			logonUserBean=(AclUserBean) act.getSession().get(Constant.LOGON_USER);
			String idStr=request.getParamValue("idStr");
			int updateNum=0;
			dao=ClaimOldPartOutStorageDao.getInstance();
			updateNum=dao.saveAllOutOfStoreLogOper(request,logonUserBean);			
			if(updateNum==idStr.split(",").length){
				act.setOutData("updateResult","updateSuccess");
			}else{
				act.setOutData("updateResult","updateFailure");
			}
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.ADD_FAILURE_CODE, "索赔旧件出库查询--全部出库操作");
			logger.error(logonUserBean, e1);
			act.setException(e1);
		}
	}
}
